<?= $this->element('reports_submenu', array('page'=>'traffic')) ?>

<?= $this->Form->create('Filter', array('url'=>'/reports/traffic', 'class'=>'filter')) ?>
    <?= $this->Form->submit(__('Filter'), array('div'=>false)) ?>
    
    <?= $this->App->input('start', 'date', @$filter['start'], array(
        'label' => __('from'),
        'labelOptions' => array(
            'class' => 'inline auto'
        )
    )) ?>
    
    <?= $this->App->input('end', 'date', @$filter['end'], array(
        'label' => __('to'),
        'labelOptions' => array(
            'class' => 'inline auto',
            'style' => 'margin-left:20px'
        )
    )) ?>
    
    <?= $this->App->input('type', 'select', @$filter['type'], array(
        'options' => array('income', 'expense'),
        'empty' => '',
        'label' => __('type'),
        'labelOptions' => array(
            'class' => 'inline auto',
            'style' => 'margin-left:20px'
        )
    )) ?>
    
<?= $this->Form->end() ?><br clear="all">

<? if (! empty($data)):

    $balance = $initState;
    $total   = 0;

?>
<table>
<caption>
    <i><?= __('Initial cash state on') ?> <?= date_local($filter['start']) ?>:</i><br>
    <b><?= currency_format($initState, true) ?></b>
</caption>
<thead>
    <tr>
        <th width="8%"><?= __('type') ?></th>
        <th width="12%"><?= __('date') ?></th>
        <th width="30%"><?= __('payer') ?>/<?= __('receiver') ?></th>
        <th><?= __('description') ?></th>
        <th><?= __('notes') ?></th>
        <th><?= __('amount') ?></th>
        <th><?= __('balance') ?></th>
    </tr>
</thead>
<tbody>
    <? foreach ($data as $row):
        $amount = (float)$row['Finance']['amount'];
        
        switch ($row['Finance']['type']) {
            case 'income':
                $balance += $amount;
                $total   += $amount;
                break;
                
            case 'expense':
                $balance -= $amount;
                $total   -= $amount;
                break;
        }
     ?>
    <tr>
        <td><?= __($row['Finance']['type']) ?></td>
        <td><?= date_local($row['Finance']['date']) ?></td>
        <td><?= $row['Finance']['entity'] ?></td>
        <td><?= $row['Finance']['description'] ?></td>
        <td><?= $row['Finance']['notes'] ?></td>
        <td align="right" class="<?= $row['Finance']['type'] ?>"><?= currency_format($amount) ?></td>
        <td align="right"><?= currency_format($balance) ?></td>
    </tr>
    <? endforeach ?>
</tbody>
</table>
<center>
    <p><i><?= __('financial result') ?>:</i><br><b><?= currency_format($total, true) ?></b></p>
    <p><i><?= __('cash flow result') ?>:</i><br><b><?= currency_format($balance, true) ?></b></p>
</center>
<? endif ?>
